package com.zhn.usermanager.alibabapay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author thinknovo
 * @version 1.0
 */
@Controller
public class TestCodePayController {
    @RequestMapping("/codePay")
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, AlipayApiException {
        String json = request.getReader().readLine();
        System.out.println("json=" + json);
        String serverURL = "https://openapi.alipaydev.com/gateway.do";     // alipay测试支付地址   生产环境支付地址为https://openapi.alipay.com/gateway.do
        String APP_ID = "2021002178606377";   // 创建的应用ID
        // 自己APP的私钥
        String mKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDVB44/1PZgdpdxOphgv1OgxjVMV9DA6d7eRfki+5+hlmY4S4jLg3yWToAmDL8X1VtxRUjW73Whxc1sxBXbHhAGh+R8Z2nhVv0wq4JjlIaon43uxkhcppmKchhEfnmvzxsZTDt7sJ7rNyS4IDgxaI5eCro8+NzTBWh0M5AWF0TM7Y4rktSGF6/cGsERbYz2rYRUYZMQVEpZY5fGdHX2NufCOtfZq6oCH6MBGIAbD+4r4Z8ea9HOd9eVvlu07v8Xb+G2F9Swk7/YppPm6Cdhv0FZyyBbUteAiAgTbJo9PttqRD8Ao9ty1f/fwhm/J/yrjHjySP/Gu44BqPkFlHtxsII9AgMBAAECggEBAJa5NtNA+WT841+wppllJWVOd+1hAIlNr2sytPgkcihhAkf4YXwktuSd/Btvz1Csm3tEGyo/2Mmx6wt2nUQgPBRy152RiZ9u7csevi14q+HoqgJz9kTfdyk2WqczmSNj9HdssXVtXLNtm6RO35NNt9Uf3+894PfJDsOQq/SFWB9o9kN++4kJD03BllhoiK3NZ91wgxDL3eUhZj52jImLd5qzhpstWuRG/+CCcN14n/s0JkMpBJHGqzFsYeXO87+4s15+Hm+3BwBtQSJYhKUEYK63gH6giTR8eOvigil42gM3dpJSGdZ7wLeeI6nlLLq0cPJlmv131UgFABjqDvC3XaECgYEA6zOwgTOlVeqJKOTeiDGx4k//BtkQnmWKrLm1+EOY8tLL0/MhBbqJ1qYKXNQm6J1z5P6VqD9/KfurQsRfFM0eTs26HpsVmtTmV5U3fKHP99swlp8eBe6wCkTdutxN7gdeNEcNzwY6IsYcrQ4FvQn4OpmOYsov6OTtSH/+J3IpyckCgYEA593yBXRrUNidXrkpLdfkQ74Z938z0qNATRBIvwKCp6s/2/mOTT0Z/CtuD9gDnnncyJOR/cuSxeHkqLs0BZIuns9K9muWzuK5Km2PyjdRtdpy5vBVs8SBa8a7iWEXEZeasu1CIShbGLaQFxQkOiTfEhSnILLsw9kbEYfShKIdrtUCgYAC8V6f/zluXcThkhuGsLh9EGssdsLSeIrHpns0ZAQh5alPOZeMLs1Stjki+DSUbIPqtZTByimZKOD3sOVPqVFbww81GtOmYEuRhPhwwsWLYF63Q75obJgfSMVNdTw0Yjs3hDwT/wvzK29oauh/eQR2Fz3E1qrKyzJ82b0gWTlUoQKBgBs6ZQFe+nsz9Mb612savyN1X3dA6MUDhrmGAATuNomaApXCaIOJeVOzhek2ZLZneRXUvoAfN80lCcUGtsUwtWjQzuzBFaSV20iwNQJ6HI5OaM/VyTqjCM+vNrabQCpAs1nvoqRiKNBuH92V7+skCU4GPtoIKOWWySb13DLXFsVBAoGAL36aF44Qviw909QqMnBsPe7rDB1ENN0+lHvFvXscQjBsGyEJoOwsUn2QGTQjaUvIq13o3Cc0zC7rpsVhndJjGQ29dfJzCk0L60RAhrLL7lw+HVZAuEi3MsBdBftTFAjJbpYIDDOJlqlCrApz5thRIZN5W5Js9HNlL0N9VA4e8rk=";
        // 支付宝的公钥(使用自己APP的公钥兑换)
        String aKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuYxoQWAmYwNhOS/BoaCsQp67jgNvFJRVdkCRfVht1w+WzNRcFXzjfoOo7gqmCPp987AoROPFfivfXU0vvfMbm7mBPEdhCTMwc23W0l6eXtts2Lp51KM4A6yRJFLFHXFKCLj8yctCPUhG+IPxsI7CDUsBm9nr+TYqxu7HgIvHVFUxTEswHh2fNWfsaBsAHe/tTLYvAKKiz+JGwP5V33YceqTt4GQcdQh01KxguZGs+ot1OicZC6DfJIyX4dqdfrOUcewm+49I91x/DLhPwywJBf08kfV6CYtjkw0UZfHonRhM9ilA1xkgtThPwYWyZ6wIRNZAf168XfGaRfosXh/77wIDAQAB";
        AlipayClient alipayClient = new DefaultAlipayClient(serverURL, APP_ID, mKey, "json", "utf-8", aKey, "RSA2"); //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();    // 创建API对应的request
        alipayRequest.setReturnUrl("http://localhost:8080/success.html");          // 当调用接口成功，返回的页面
        alipayRequest.setNotifyUrl("http://localhost:8080/payReturnServlet");   // 在公共参数中设置回跳和通知地址(必须在公网IP)

        /*
             填充业务参数
                 out_trade_no      每笔订单号需要自身的唯一id，支付宝会根据此id判断是否交易
                 product_code
                 total_amount      这笔订单的价格
                 subject           订单商品名称(也可以为标题)
                 body              订单描述
                 passback_params   回传参数
                 extend_params
                 product_code   支付方式：QUICK_WAP_PAY 登录账号支付
         */
        // 接收订单号 例如："20190210010118666"
        String out_trade_no = request.getParameter("dingdan_no");          // 每笔订单号需要自身的唯一id，支付宝会根据此id判断是否交易
        String product_code = "FAST_INSTANT_TRADE_PAY";                      // 支付方式：FAST_INSTANT_TRADE_PAY扫码支付
        String total_amount = "13886.88";                                    // 这笔订单的价格
        String subject = "IphoneXXXXXSSSSS 1TB";                             // 订单商品名称
        String body = "这是一个用肾换来的电话";                                // 订单商品描述

        String bizContent = "{" +
                "\"out_trade_no\":\"" + out_trade_no + "\"," +
                "\"product_code\":\"" + product_code + "\"," +
                "\"total_amount\":" + total_amount + "," +
                "\"subject\":\"" + subject + "\"," +
                "\"body\":\"" + body + "\"}";                                // 生成支付json数据
        System.out.println("支付生成json数据 bizContent=" + bizContent);      // 打印json支付数据

        alipayRequest.setBizContent(bizContent);                             // 提交支付请求

        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody();        //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(form);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }
}
